/*
 *  
 */

package net.ubinow.mflex.widgets;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import net.ubinow.mflex.baseui.WidgetContainer;



/**
 * Specific tab to the TabView; will encapsulate the tab image, selected table image, basically a container/data 
 * class that has no logic - cycling, keypress handling, and painting are all done by the TabView class.
 * @author Josh
 */
public class Tab {

    private Image _selTabIcon = null;
    private Image _tabIcon = null;
    private String _tabTitle = null;
    private String _id = null;      
    
    private int _x = 0;
    private int _y = 0;
    private int _width = 0;
    private int _height = 0;
    
    private int _tabTitleX = 0; // default to the centre 
    private int _tabTitleY = 0; // default to the middle 
    
    private int _tabIconX = 0; // default to the centre 
    private int _tabIconY = 0; // default to the middle
    
    private TabView _parentView = null; // will be assigned the listener for all evetns
    
    private WidgetContainer _widgetContainer = null;
    
    //<editor-fold defaultstate="collapsed" desc="constructors">
    public Tab( String id, TabView parentView, Image tabIcon, Image selTabIcon, int x, int y, int width, int height ){
        this( id, parentView, tabIcon, selTabIcon, null, x, y, width, height );
    }
    
    public Tab( String id, TabView parentView, String tabTitle, int x, int y, int width, int height ){
        this( id, parentView, null, null, tabTitle, x, y, width, height );
    }
    
    public Tab( String id, TabView parentView, Image tabIcon, Image selTabIcon, String tabTitle, 
            int x, int y, int width, int height ){
        
        _id = id;
        _parentView = parentView;
        _tabIcon = tabIcon;
        _selTabIcon = selTabIcon;
        _tabTitle = tabTitle;
        
        // positioning values
        _x = x;
        _y = y;
        _width = width; 
        _height = height;
     
        _widgetContainer = new WidgetContainer( _x, _y, _width, _height );           
    }   
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="getters and setters">
    
    public int getX() {
        return _x;
    }

    public void setX(int x) {
        this._x = x;
    }

    public int getY() {
        return _y;
    }

    public void setY(int y) {
        this._y = y;
    }

    public int getWidth() {
        return _width;
    }

    public void setWidth(int width) {
        this._width = width;
    }

    public int getHeight() {
        return _height;
    }

    public void setHeight(int height) {
        this._height = height;
    }

    public Image getSelTabIcon() {
        if( _selTabIcon != null )
            return _selTabIcon;
        else
            return _tabIcon;
    }

    public void setSelTabIcon(Image selTabIcon) {
        this._selTabIcon = selTabIcon;
    }

    public Image getTabIcon() {
        return _tabIcon;
    }
    
    public boolean hasTabIcon(){
        return _tabIcon != null;
    }

    public void setTabIcon(Image tabIcon) {
        this._tabIcon = tabIcon;
    }

    public String getTabTitle() {
        return _tabTitle;
    }
    
    public boolean hasTabTitle(){
        if( _tabTitle == null || _tabTitle.trim().length() == 0 )
            return false;
        else
            return true;                     
    }

    public void setTabTitle(String tabTitle) {
        this._tabTitle = tabTitle;
    }

    public String getId() {
        return _id;
    }

    public void setId(String id) {
        this._id = id;
    }
    
    public int getTabTitleX() {
        return _tabTitleX;
    }

    public void setTabTitleX(int textX) {
        this._tabTitleX = textX;
    }

    public int getTabTitleY() {
        return _tabTitleY;
    }

    public void setTabTitleY(int textY) {
        this._tabTitleY = textY;
    }

    public int getTabIconX() {
        return _tabIconX;
    }

    public void setTabIconX(int iconX) {
        this._tabIconX = iconX;
    }

    public int getTabIconY() {
        return _tabIconY;
    }

    public void setTabIconY(int iconY) {
        this._tabIconY = iconY;
    }

    public WidgetContainer getWidgetContainer() {
        return _widgetContainer;
    }

    public void setWidgetContainer(WidgetContainer widgetContainer) {
        this._widgetContainer = widgetContainer;
    }    
    
    //</editor-fold>
    
    public void cycle(){
        if( _widgetContainer != null )
            _widgetContainer.cycle();
    }
    
    public boolean handleKeyPressed(int action, int keyCode) {
        if( _widgetContainer == null )
            return false;
        
        if( action == Canvas.DOWN || action == Canvas.RIGHT )
            _widgetContainer.setLoopingWidgetNav( true ); 
        else
            _widgetContainer.setLoopingWidgetNav( false ); // make sure that the widget does not cycle it's selected from top-bottom and bottom-top
        
        return _widgetContainer.handleKeyPressed( action, keyCode );
    }

    public boolean handleKeyReleased(int action, int keyCode) {
        if( _widgetContainer == null )
            return false;               
        
        return _widgetContainer.handleKeyReleased( action, keyCode );
    }
    
    public void setFocus( boolean focus, int direction ){
        if( _widgetContainer == null )
            return; 
        
        _widgetContainer.setFocus( focus, direction );
    }
    
    public void paint( Graphics g, int offsetX, int offsetY, int layer ){
        if( _widgetContainer == null )
            return;
        
        _widgetContainer.paint( g, offsetX, offsetY, layer );
    }        
    
    public boolean supportsTransitioning(){
        if( _widgetContainer == null )
            return false; 
        else
            return _widgetContainer.supportsTransitioning();
    }
}
